$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'jxc/jxcsaleorder/list',
        datatype: "json",
        colModel: [
			{ label: '商户', name: 'shopName', index: 'shop_name', width: 70 },
			{ label: '收银员', name: 'sellWorerName', index: 'sell_worer_name', width: 50 },
			{ label: '交易单号', name: 'sellMasterCode', index: 'sell_master_code', width: 200 },
			{ label: '数量', name: 'totalCount', index: 'total_count', width: 40 },
			{ label: '交易金额', name: 'totalAmount', index: 'total_amount', width: 50 },
			{ label: '销售时间', name: 'sellMasterDate', index: 'sell_master_date', width: 100 },
			{ label: '详情', name: '', index: 'id', width: 40,formatter: function(value, options, row){
                        return '<span class="label label-info" name="'+row.sellMasterCode+'" shopCode="'+row.shopCode+'" sellMasterDate="'+row.sellMasterDate+'" style="cursor: pointer" onclick="getDetails(this)">查看</span>';
                }},

        ],
		viewrecords: true,
        height: 385,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 80,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            var heig = $('.larry-iframe', parent.document).css("height");
            heig = heig.substring(0,heig.length-2);
            $("#gbox_jqGrid").css("height",(Number(heig)-115)+"px");
            $("#gview_jqGrid").css("height",(Number(heig)-165)+"px");
            $(".ui-jqgrid-bdiv").css("height",(Number(heig)-195)+"px");

        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		jxcSaleOrder: {},
        q:{sellMasterCode:null}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.jxcSaleOrder = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.jxcSaleOrder.id == null ? "jxc/jxcsaleorder/save" : "jxc/jxcsaleorder/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.jxcSaleOrder),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "jxc/jxcsaleorder/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "jxc/jxcsaleorder/info/"+id, function(r){
                vm.jxcSaleOrder = r.jxcSaleOrder;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
			    postData:{sellMasterCode:vm.q.sellMasterCode},
                page:page
            }).trigger("reloadGrid");
		}
	}
});

function getDetails(obj) {
    var sellMasterCode = $(obj).attr("name");
    var shopCode = $(obj).attr("shopCode");
    var sellMasterDate = $(obj).attr("sellMasterDate");
    getSellDetail(sellMasterCode);
    startVideo(shopCode,sellMasterDate);

}

function getSellDetail(sellMasterCode) {
    $.get(baseURL + "jxc/jxcselldetails/getBySellMasterCode/"+sellMasterCode, function(r){
        $("#detail").empty();
        let info="";
        for (let i = 0; i <r.sellList.length ; i++) {
            info+="<tr><td>"+r.sellList[i].commodityName+"</td><td>"+r.sellList[i].sellCount+"</td><td>"+r.sellList[i].price+"</td><td>"+r.sellList[i].discount+"</td><td>"+r.sellList[i].sellAmount+"</td></tr>"
        }
        $("#detail").html(info);
    });
}
//银监回放
function startVideo(shopCode,sellMasterDate) {
    //查询通道号
    $.get(baseURL + "jxc/jxcshop/infoByCode?shopCode="+shopCode+"&sellMasterDate="+sellMasterDate, function(r){
        var channelNumber=r.jxcShop.channelNumber;
        var vlc = document.getElementById('vlc');
        var id=vlc.playlist.add("rtsp://admin:YXFWQ123@192.168.1.250:554/Streaming/tracks/"+channelNumber+"?starttime="+r.startTime+"&endtime="+r.endTime);
        vlc.playlist.playItem(id);
    });
}

